<?php
/**
 * Created by PhpStorm.
 * User: George
 * Date: 2017/9/25
 * Time: 11:06
 */

namespace AdminBundle\Task;
use AdminBundle\Entity\LogNewMember;

/**
 * 响应包体格式
 * {
 * "code":"0",
 * "msg":"请求成功",
 * "data":[{"aliasKey":"18610138374","name":"\u82b1\u4ed9\u5b50","mobile":"18610138374"},{"aliasKey":"18611470608","name":"feifei\u8d1d","mobile":"18611470608"},{"aliasKey":"18610197374","name":"\u843d\u53f6\u98de\u821e","mobile":"18610197374"},{"aliasKey":"18610138654","name":"\u6000\u5ff5\u65f6\u4ee3","mobile":"18610138654"},{"aliasKey":"18611257414","name":"\u660c\u660c\u5988\u54aa","mobile":"18611257414"}]
 * }
 * 添加熟人好友
 * Class NewContactOfFriend
 * @package AdminBundle\Task
 */
class NewContactOfFriendTask extends Task
{
    // "sourceType": "0:扫一扫, 1:搜索微信号, 2:搜索手机号, 3:通讯录"
    const TYPE_QRCODE = 0;
    const TYPE_SEARCH_ID = 1;
    const TYPE_SEARCH_MOBILE = 2;
    const TYPE_CONTACTS = 3;

    const SOURCE_TYPE_FRIEND = 1; //熟人好友
    private $contacts;

    /**
     * 返回
     * @return null
     */
    public function jsonTask()
    {
        $data = [
            'app' => 'api', 'os' => 'ios', 'deviceid' => $this->task->getDeviceName(),
            'data' =>
                [
                    'token' => $this->task->getAction(),
                    'webchat' => [$this->task->getWebchatUsername(), $this->task->getNote()]
                ]
        ];

        $url = 'http://admin.bjtwtx.com/api/partner/wxbest';

        $json = $this->httpJson($url, $data);

        if ($json == null || $json['code'] != 0) {
            return null;
        }
        $contacts = $json['data'];
        if (empty($contacts))
            return null;
        $this->contacts = $contacts;

        $friends = [];
        foreach ($contacts as $contact) {
            $friends[] = $contact['mobile'];
        }
        $this->logNewMember($this->em);
        
        // "sourceType": "0:扫一扫, 1:搜索微信号, 2:搜索手机号, 3:通讯录"
        return json_encode([
            'sourceType' => self::TYPE_CONTACTS, //3:通讯录
            'helloMsg' => $this->getHelloMsg(),
            'delay' => 3,
            'index' => 0,
            'friends' => $friends
        ]);
    }

    /**
     * 执行数据库操作
     * @param $em
     */
    public function logNewMember($em)
    {
        if (empty($this->contacts))
            return;
        foreach ($this->contacts as $contact) {
            $logNewMember = new LogNewMember();

            $logNewMember->setCtime(new \DateTime())
                ->setWebchatId($this->task->getMethod())
                ->setWebchatUsername($this->task->getWebchatUsername())
                ->setNote($this->task->getNote())
                ->setMobile($contact['mobile'])
                ->setUserName($contact['aliasKey'])
                ->setNickname($contact['name'])
                ->setTags('')
                ->setSourceType(self::SOURCE_TYPE_FRIEND);

            $em->persist($logNewMember);
        }
        $em->flush();
    }

    private function getHelloMsg()
    {
        $sentence = [
            ['id' => 0, 'content' => '  '],
            ['id' => 1, 'content' => '你好'],
            ['id' => 2, 'content' => '我是妈网群里的'],
            ['id' => 3, 'content' => '来自通讯录好友'],
            ['id' => 4, 'content' => '来自QQ好友'],
        ];
        $rand_key = array_rand($sentence);

        return $sentence[$rand_key]['content'];
    }

}